package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.utils.SMSUtils;
import com.itheima.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/sendMsg")
    public R sendMsg(@RequestBody User user, HttpServletRequest request){

        //1. 生成一个验证码 (随机数)
        Integer code = ValidateCodeUtils.generateValidateCode(4);
        log.info("本次生成验证码为 : {}", code);

        String phone = user.getPhone();
        if(StringUtils.isNotEmpty(phone)){
            //2. 发送短信
            //SMSUtils.sendMessage("","", phone, code.toString());

            //3. 存储验证码
//            request.getSession().setAttribute(phone,code);
            redisTemplate.opsForValue().set(phone,code.toString(),5, TimeUnit.MINUTES);
            //4. 返回
            return R.success();
        }

        return R.error("手机号非法!");
    }


    @PostMapping("/login")
    public R login(@RequestBody Map map, HttpServletRequest request){
        log.info("执行登录操作, map={}",map);

        //1. 获取页面验证码及session中验证码
        String code = map.get("code").toString();
        String phone = map.get("phone").toString();

//        String codeInSession = request.getSession().getAttribute(phone).toString();
        Object o = redisTemplate.opsForValue().get(phone);
        //2. 进行验证码对比校验
        if(code != null && code.equals(o)){
            //3. 如果校验通过, 登录成功, 记录用户信息 phone(手机号不存在时)
            //3.1 先根据phone查询
            LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(User::getPhone, phone);
            User user = userService.getOne(lambdaQueryWrapper);

            //3.2 不存在, 则添加
            if(user == null){
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);

                userService.save(user);
            }

            //4. 存储用户的登录信息在session , 删除验证码
            request.getSession().setAttribute("user", user.getId());
//            request.getSession().removeAttribute(phone);
            redisTemplate.delete(phone);
            return R.success(user);
        }

        return R.error("验证码错误");
    }



    // -------------------------------------- 实战功能 ---------------------------------
    @PostMapping("/loginout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().invalidate();
        return R.success("退出成功");
    }

}

